package com.aquila.bible.activity;

import java.util.Calendar;

import android.app.Activity;
import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.aquila.bible.MyBaseActivity;
import com.aquila.bible.R;
import com.aquila.bible.SuggestObject;
import com.aquila.bible.system.Utils;
import com.util.mail.MailSenderInfo;
import com.util.mail.SimpleMailSender;

public class FeedBackActivity extends MyBaseActivity implements OnClickListener {
	
	private EditText contentEditText ;
	private EditText phoneNumberEditText ;
	private EditText qqNumberEditText ;
	private Spinner spinner ;
	TextView tView ;
	private Button submitButton ;
	private LinearLayout processLayout;
	private String[] suggessType  ;
	private Dialog sendingDialog ;
	private ArrayAdapter<String> arrayAdapter;
	private int typeIndex ; 
	private TelephonyManager tm ;
	private String mail1 = "tianlan198889@126.com" ;
	private String mail2 = "tianlan198889@163.com" ;
	
//	private int sel
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_feedback) ;
		contentEditText = (EditText) findViewById(R.id.et_suggest) ;
		phoneNumberEditText = (EditText) findViewById(R.id.et_suggest_phoneNum) ;
		qqNumberEditText = (EditText) findViewById(R.id.et_suggest_qqNum) ;
		spinner = (Spinner) findViewById(R.id.spinner_feedback_type) ;
		submitButton = (Button) findViewById(R.id.btn_submit) ;
		submitButton.setOnClickListener(this) ;
		processLayout = (LinearLayout) layoutInflater.inflate(R.layout.process_view, null) ;
		tView = (TextView) processLayout.findViewById(R.id.top_process_promot) ;
		tView.setText("正在发送中...") ;
		
		suggessType = getResources().getStringArray(R.array.suggess_type) ;
		tm = (TelephonyManager) getSystemService(Activity.TELEPHONY_SERVICE) ;
		
		arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, suggessType) ;
		arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) ;
		spinner.setAdapter(arrayAdapter) ;
		
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				typeIndex = arg2 ;
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		}) ;
		
		sendingDialog = new Dialog(context, R.style.my_dialog) ;
		sendingDialog.setContentView(processLayout) ;
		sendingDialog.setCanceledOnTouchOutside(false) ;
		
	}
	
	
	private void papearSend(String content){
		handler.sendEmptyMessage(1) ;
		SuggestObject suggestObject = new SuggestObject() ;
		suggestObject.setTypeString(suggessType[typeIndex]) ;
		suggestObject.setPhoneNumberString(phoneNumberEditText.getText().toString()) ;
		suggestObject.setQqNumberString(qqNumberEditText.getText().toString()) ;
		suggestObject.setContentString(content) ;
		sendMail(suggestObject) ;
		
		
	}

	
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				sendingDialog.show() ;
			}
			else if (msg.what == 2) {
				showToast("发送成功,感谢您提交的反馈意见。", true) ;
				sendingDialog.dismiss() ;
			}
			else if (msg.what == 3) {
				showToast("发送失败，请检查您手机的网络设置。", true) ;
				sendingDialog.dismiss() ;
			}
		};
	} ;
	
	
	@Override
	public void onClick(View v) {
		if (v == submitButton) {
			if (Utils.getNetworkState(context) == Utils.NETWORN_NONE) {
				showToast("您的网络没有开启，不能发送反馈意见，请开启您的手机网络设置。", true) ;
				return ;
			}
			final String content = contentEditText.getText().toString() ;
			if (content.length() < 5) {
				showToast("您反馈的内容输入过少，请继续输入", false) ;
				return ;
			}
			
			new Thread(){
				public void run() {
					papearSend(content) ;
				};
			}.start() ;
		}
	}
	
	
	
	
	
	
	/**
	 * 发送邮件
	 * @param e
	 *2012-7-19 上午10:43:00
	 */
	private void sendMail(SuggestObject suggestObject){
		MailSenderInfo mailInfo = new MailSenderInfo();
		mailInfo.setMailServerPort("25");
		mailInfo.setValidate(true);
		if (Calendar.getInstance().getTimeInMillis() / 86400000 % 2 == 0) {
			mailInfo.setMailServerHost("smtp.126.com");
        	mailInfo.setUserName(mail1) ;
        	mailInfo.setFromAddress(mail1);
        }
		else {
			mailInfo.setMailServerHost("smtp.163.com");
			mailInfo.setUserName(mail2) ;
			mailInfo.setFromAddress(mail2);
		}
		mailInfo.setPassword("123456");// 您的邮箱密码
		mailInfo.setToAddress("biblefeedback@gmail.com");
		mailInfo.setSubject(suggestObject.getTypeString());
		
		String content ="手机型号\t" + Build.MODEL  + "\n\n\n" +
					"手机序列号：\t" + tm.getDeviceId() +"\n\n\n"+
				"手机号码：\t" + suggestObject.getPhoneNumberString() +"\n\n\n"+
						"Q Q号码: \t\t" + suggestObject.getQqNumberString() +"\n\n\n" +
						"意见内容：\t\t\t" + suggestObject.getContentString() ;
		
		mailInfo.setContent(content);
		
		// 这个类主要来发送邮件
		SimpleMailSender sms = new SimpleMailSender();
		boolean isSuccess = sms.sendTextMail(mailInfo);// 发送文体格式
		if (isSuccess) {
			handler.sendEmptyMessage(2) ;
		}
		else {
			handler.sendEmptyMessage(3) ;
		}
	}	
	
	
	
}
